/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "Input.h"

#include "intern/DxInput.h"




uint8 S_InputKey::e_ESC = DIK_ESCAPE;


uint8 S_InputKey::e_KEY_1 = DIK_1;
uint8 S_InputKey::e_KEY_2 = DIK_2;
uint8 S_InputKey::e_KEY_3 = DIK_3;
uint8 S_InputKey::e_KEY_4 = DIK_4;
uint8 S_InputKey::e_KEY_5 = DIK_5;
uint8 S_InputKey::e_KEY_6 = DIK_6;
uint8 S_InputKey::e_KEY_7 = DIK_7;
uint8 S_InputKey::e_KEY_8 = DIK_8;
uint8 S_InputKey::e_KEY_9 = DIK_9;
uint8 S_InputKey::e_KEY_0 = DIK_0;

uint8 S_InputKey::e_KEY_减 = DIK_MINUS;
uint8 S_InputKey::e_KEY_加 = DIK_EQUALS;
uint8 S_InputKey::e_KEY_退格 = DIK_BACK;

uint8 S_InputKey::e_KEY_TAB = DIK_TAB;

uint8 S_InputKey::e_KEY_A = DIK_A;
uint8 S_InputKey::e_KEY_B = DIK_B;
uint8 S_InputKey::e_KEY_C = DIK_C;
uint8 S_InputKey::e_KEY_D = DIK_D;
uint8 S_InputKey::e_KEY_E = DIK_E;
uint8 S_InputKey::e_KEY_F = DIK_F;
uint8 S_InputKey::e_KEY_G = DIK_G;
uint8 S_InputKey::e_KEY_H = DIK_H;
uint8 S_InputKey::e_KEY_I = DIK_I;
uint8 S_InputKey::e_KEY_J = DIK_J;
uint8 S_InputKey::e_KEY_K = DIK_K;
uint8 S_InputKey::e_KEY_L = DIK_L;
uint8 S_InputKey::e_KEY_M = DIK_M;
uint8 S_InputKey::e_KEY_N = DIK_N;
uint8 S_InputKey::e_KEY_O = DIK_O;
uint8 S_InputKey::e_KEY_P = DIK_P;
uint8 S_InputKey::e_KEY_Q = DIK_Q;
uint8 S_InputKey::e_KEY_R = DIK_R;
uint8 S_InputKey::e_KEY_S = DIK_S;
uint8 S_InputKey::e_KEY_T = DIK_T;
uint8 S_InputKey::e_KEY_U = DIK_U;
uint8 S_InputKey::e_KEY_V = DIK_V;
uint8 S_InputKey::e_KEY_W = DIK_W;
uint8 S_InputKey::e_KEY_X = DIK_X;
uint8 S_InputKey::e_KEY_Y = DIK_Y;
uint8 S_InputKey::e_KEY_Z = DIK_Z;

uint8 S_InputKey::e_KEY_方括号左 = DIK_LBRACKET;
uint8 S_InputKey::e_KEY_方括号右 = DIK_RBRACKET;


uint8 S_InputKey::e_KEY_回车 = DIK_RETURN;
uint8 S_InputKey::e_KEY_点 = DIK_PERIOD;

uint8 S_InputKey::e_KEY_分号 = DIK_SEMICOLON;
uint8 S_InputKey::e_KEY_引号 = DIK_APOSTROPHE;
uint8 S_InputKey::e_KEY_反斜杠 = DIK_BACKSLASH;


uint8 S_InputKey::e_KEY_空格 = DIK_SPACE;


uint8 S_InputKey::e_KEY_F1 = DIK_F1;
uint8 S_InputKey::e_KEY_F2 = DIK_F2;
uint8 S_InputKey::e_KEY_F3 = DIK_F3;
uint8 S_InputKey::e_KEY_F4 = DIK_F4;
uint8 S_InputKey::e_KEY_F5 = DIK_F5;
uint8 S_InputKey::e_KEY_F6 = DIK_F6;
uint8 S_InputKey::e_KEY_F7 = DIK_F7;
uint8 S_InputKey::e_KEY_F8 = DIK_F8;
uint8 S_InputKey::e_KEY_F9 = DIK_F9;
uint8 S_InputKey::e_KEY_F10 = DIK_F10;
uint8 S_InputKey::e_KEY_F11 = DIK_F11;
uint8 S_InputKey::e_KEY_F12 = DIK_F12;



//uint8 S_InputKey::e_KEY_退格 = DIK_BACK;
//uint8 S_InputKey::e_KEY_BACKQUOTE = DIK_BACKSLASH;
uint8 S_InputKey::e_KEY_逗号 = DIK_COMMA;
uint8 S_InputKey::e_KEY_句号 = DIK_PERIOD;
uint8 S_InputKey::e_KEY_斜杠 = DIK_SLASH;

uint8 S_InputKey::e_KEY_方向_右 = DIK_RIGHT;
uint8 S_InputKey::e_KEY_方向_左 = DIK_LEFT;
uint8 S_InputKey::e_KEY_方向_下 = DIK_DOWN;
uint8 S_InputKey::e_KEY_方向_上 = DIK_UP;
	
uint8 S_InputKey::e_KEY_小键盘数字键_除 = DIK_DIVIDE;
uint8 S_InputKey::e_KEY_小键盘数字键_乘 = DIK_MULTIPLY;
uint8 S_InputKey::e_KEY_小键盘数字键_减 = DIK_MEDIASTOP;
uint8 S_InputKey::e_KEY_小键盘数字键_加 = DIK_ADD;
uint8 S_InputKey::e_KEY_小键盘数字键_回车 = DIK_NUMPADENTER;
	
uint8 S_InputKey::e_KEY_小键盘数字键_1 = DIK_NUMPAD1;
uint8 S_InputKey::e_KEY_小键盘数字键_2 = DIK_NUMPAD2;
uint8 S_InputKey::e_KEY_小键盘数字键_3 = DIK_NUMPAD3;
uint8 S_InputKey::e_KEY_小键盘数字键_4 = DIK_NUMPAD4;
uint8 S_InputKey::e_KEY_小键盘数字键_5 = DIK_NUMPAD5;
uint8 S_InputKey::e_KEY_小键盘数字键_6 = DIK_NUMPAD6;
uint8 S_InputKey::e_KEY_小键盘数字键_7 = DIK_NUMPAD7;
uint8 S_InputKey::e_KEY_小键盘数字键_8 = DIK_NUMPAD8;
uint8 S_InputKey::e_KEY_小键盘数字键_9 = DIK_NUMPAD9;
uint8 S_InputKey::e_KEY_小键盘数字键_0 = DIK_NUMPAD0;
uint8 S_InputKey::e_KEY_小键盘数字键_点 = DIK_DECIMAL;
	 

uint8 S_InputKey::e_KEY_大写锁定 = DIK_CAPITAL;
uint8 S_InputKey::e_KEY_左SHIFT = DIK_LSHIFT;
uint8 S_InputKey::e_KEY_左CTRL = DIK_LCONTROL;
uint8 S_InputKey::e_KEY_左ALT = DIK_LMENU;
uint8 S_InputKey::e_KEY_右SHIFT = DIK_RSHIFT;
uint8 S_InputKey::e_KEY_右CTRL = DIK_RCONTROL;
uint8 S_InputKey::e_KEY_右ALT = DIK_RMENU;
//DIK_RWIN

uint8 S_InputKey::e_KEY_上一页 = DIK_PRIOR;
uint8 S_InputKey::e_KEY_下一页 = DIK_NEXT;

uint8 S_InputKey::e_KEY_删除 = DIK_DELETE;


uint8 S_InputKey::e_KEY_左系统徽标 = DIK_LWIN;
uint8 S_InputKey::e_KEY_右系统徽标 = DIK_RWIN;
uint8 S_InputKey::e_KEY_菜单 = DIK_APPS;




S_输入环境* f_input_create输入设备() {
	S_输入环境* inputCtx = (S_输入环境*)malloc(sizeof(S_输入环境));
	inputCtx->m_Key.Keys = (uint8*)(calloc(256, sizeof(uint8)));
	inputCtx->m_Key.Keys状态 = (uint8*)(calloc(256, sizeof(int8)));
	inputCtx->m_鼠标全局坐标 = {};

	inputCtx->m_设备是否激活 = false;
	inputCtx->m_InputDev = new CDInputKeyboard;

	inputCtx->m_鼠标设备 = nullptr;
	inputCtx->m_键盘设备 = nullptr;
	inputCtx->m_鼠标 = (S_结构指针)malloc(sizeof(S_鼠标属性));

	return inputCtx;
}

void f_input_release输入设备(S_输入环境* ctx) {
	((CDInputKeyboard*)ctx->m_InputDev)->f_释放设备(&(ctx->m_鼠标设备));
	((CDInputKeyboard*)ctx->m_InputDev)->f_释放设备(&(ctx->m_键盘设备));

	free(ctx->m_Key.Keys);
	free(ctx->m_Key.Keys状态);
	delete ctx->m_InputDev;
	free(ctx);
}

void f_initInput(S_输入环境* ctx, S_结构指针 窗口勾丙, S_结构指针 窗口实例) {
	//((CDInputKeyboard*)ctx->m_InputDev)->InitInput((HWND)窗口勾丙, *((HINSTANCE*)窗口实例));
	((CDInputKeyboard*)ctx->m_InputDev)->InitInput((HWND)窗口勾丙, (HINSTANCE)窗口实例);

	uint8 num = ((CDInputKeyboard*)ctx->m_InputDev)->EnumKeyboard();

	((CDInputKeyboard*)ctx->m_InputDev)->f_获取设备(&(ctx->m_鼠标设备), E_输入设备类型预设::e_In鼠标);
	((CDInputKeyboard*)ctx->m_InputDev)->f_获取设备(&(ctx->m_键盘设备), E_输入设备类型预设::e_In键盘);
}


void f_input_取输入状态(S_输入环境* ctx) {
	((CDInputKeyboard*)ctx->m_InputDev)->f_get鼠标状态(&ctx->m_鼠标设备, ctx->m_鼠标);
	((CDInputKeyboard*)ctx->m_InputDev)->f_get键盘状态(ctx);

	//ctx->m_鼠标全局坐标.x += ((S_鼠标属性*)ctx->m_鼠标)->lX;
	//ctx->m_鼠标全局坐标.y += ((S_鼠标属性*)ctx->m_鼠标)->lY;
}














uint8 f_getKeyDown(const S_键盘& ctx) {
	for (uint16 i = 0; i < 256; ++i) {
		if(ctx.Keys[i]) return i;
	}
	return 0;
}

uint8 f_getKeyUn(const S_键盘& ctx) {
	for (uint16 i = 0; i < 256; ++i) {
		if (ctx.Keys状态[i] == DEF_按键状态_放开) return i;
	}
	return 0;
}

uint16 f_input_getDXAsciiKey(uint8 key, E_平台键值类型 类型) {

	if(类型 == E_平台键值类型::e_Win) return key;


	static HKL layout = GetKeyboardLayout(0);
	static uint8 State[256];
	uint16 ksy = 0;

	if (GetKeyboardState(State) == FALSE)
		return 0;
	UINT vk = MapVirtualKeyEx(key, 1, layout);
	ToAsciiEx(vk, key, State, &ksy, 0, layout);

	if (0 == ksy) {
		switch (key) {
			case DIK_NUMPAD1: return 49;
			case DIK_NUMPAD2: return 50;
			case DIK_NUMPAD3: return 51;
			case DIK_NUMPAD4: return 52;
			case DIK_NUMPAD5: return 53;
			case DIK_NUMPAD6: return 54;
			case DIK_NUMPAD7: return 55;
			case DIK_NUMPAD8: return 56;
			case DIK_NUMPAD9: return 57;
			case DIK_NUMPAD0: return 48;

			case DIK_DECIMAL: return 46;
			case DIK_NUMPADENTER: return '\n';
			default:
				break;
		}
	}

	return ksy;
	
}
std::u16string f_input_getDX键码名称(uint8 key) {
	static HKL layout = GetKeyboardLayout(0);
	static uint8 State[256];
	uint16 ksy = 0;

	if (GetKeyboardState(State) == FALSE)
		return 0;
	UINT vk = MapVirtualKeyEx(key, 1, layout);
	ToAsciiEx(vk, key, State, &ksy, 0, layout);

	switch (key) {
		case DIK_CAPITAL:	return u"Capslk";
		case DIK_LSHIFT:	return u"左-Shift";
		case DIK_LCONTROL:	return u"左-Ctrl";
		case DIK_LMENU:		return u"左-Alt";
		case DIK_RSHIFT:	return u"右-Shift";
		case DIK_RCONTROL:	return u"右-Ctrl";
		case DIK_RMENU:		return u"右-Alt";
		case DIK_TAB:		return u"TAB";

		case DIK_NUMPAD1: return u"小键盘-1";
		case DIK_NUMPAD2: return u"小键盘-2";
		case DIK_NUMPAD3: return u"小键盘-3";
		case DIK_NUMPAD4: return u"小键盘-4";
		case DIK_NUMPAD5: return u"小键盘-5";
		case DIK_NUMPAD6: return u"小键盘-6";
		case DIK_NUMPAD7: return u"小键盘-7";
		case DIK_NUMPAD8: return u"小键盘-8";
		case DIK_NUMPAD9: return u"小键盘-9";
		case DIK_NUMPAD0: return u"小键盘-0";

		case DIK_DECIMAL: return u"小键盘-点";
		case DIK_NUMPADENTER: return u"\n";

		case DIK_A: return u"A";
		case DIK_B: return u"B";
		case DIK_C: return u"C";
		case DIK_D: return u"D";
		case DIK_E: return u"E";
		case DIK_F: return u"F";
		case DIK_G: return u"G";
		case DIK_H: return u"H";
		case DIK_I: return u"I";
		case DIK_J: return u"J";
		case DIK_K: return u"K";
		case DIK_L: return u"L";
		case DIK_M: return u"M";
		case DIK_N: return u"N";
		case DIK_O: return u"O";
		case DIK_P: return u"P";
		case DIK_Q: return u"Q";
		case DIK_R: return u"R";
		case DIK_S: return u"S";
		case DIK_T: return u"T";
		case DIK_U: return u"U";
		case DIK_V: return u"V";
		case DIK_W: return u"W";
		case DIK_X: return u"X";
		case DIK_Y: return u"Y";
		case DIK_Z: return u"Z";
			
		default:
			return std::u16string(1, ksy);
			break;
	}
	

	return std::u16string(1, ksy);
}



int16 f_input_getDX按键小键盘值(int8 key) {
	switch (key)
	{
	case DIK_NUMPAD1: return 1;
	case DIK_NUMPAD2: return 2;
	case DIK_NUMPAD3: return 3;
	case DIK_NUMPAD4: return 4;
	case DIK_NUMPAD5: return 5;
	case DIK_NUMPAD6: return 6;
	case DIK_NUMPAD7: return 7;
	case DIK_NUMPAD8: return 8;
	case DIK_NUMPAD9: return 9;
	case DIK_NUMPAD0: return 0;
	default:
		break;
	}
	return -1;
}














